import argparse
import os.path
import socket
import sys

from xdevice.__main__ import main_process

smoke_cases = [
    [
        'PreTest_RK3568_Tooling_Calculator_0100',
        'PreTest_RK3568_Multimedia_Camera_0100',
        'PreTest_RK3568_Multimedia_Camera_0200',
        'PreTest_RK3568_Multimedia_Camera_0300',
        'PreTest_RK3568_Communication_Contact_0100',
        'PreTest_RK3568_Basic_Demo_0100',
        'PreTest_RK3568_Basic_Desktop_0100',
        'PreTest_RK3568_Multimedia_Gallery_0100'
    ],
    [
        'PreTest_RK3568_Tooling_Memorandum_0100',
        'PreTest_RK3568_Tooling_Memorandum_0900',
        'PreTest_RK3568_Multimedia_Music_0100',
        'PreTest_RK3568_Basic_Setting_0100',
        'PreTest_RK3568_Basic_Setting_0800',
        'PreTest_RK3568_Communication_SMSMMS_0100',
        'PreTest_RK3568_Basic_Systemui_0400',
        'PreTest_RK3568_Basic_Systemui_0800'
    ]
]

total_cases = [
    [
        'PreTest_RK3568_Tooling_Calculator_0100',
        'PreTest_RK3568_Tooling_Calculator_0200',
        'PreTest_RK3568_Tooling_Calculator_0300',
        'PreTest_RK3568_Multimedia_Camera_0100',
        'PreTest_RK3568_Multimedia_Camera_0200',
        'PreTest_RK3568_Multimedia_Camera_0300',
        'PreTest_RK3568_Communication_Contact_0100',
        'PreTest_RK3568_Communication_Contact_0200',
        # 联系人0300,0500,1000,0600都依赖0200的创建联系人用例
        'PreTest_RK3568_Communication_Contact_0300',
        'PreTest_RK3568_Communication_Contact_0500',
        'PreTest_RK3568_Communication_Contact_1000',
        # 0600是删除联系人的用例必须在上面的用例之前运行
        'PreTest_RK3568_Communication_Contact_0600',
        'PreTest_RK3568_Basic_Desktop_0100',
        'PreTest_RK3568_Basic_Desktop_0200',
        'PreTest_RK3568_Basic_Desktop_0300',
        # todo 需要重启的用例先下线
        # 'PreTest_RK3568_Basic_Desktop_0600',
        'PreTest_RK3568_Basic_Desktop_0700',
        'PreTest_RK3568_Basic_Desktop_1000',
        'PreTest_RK3568_Basic_Desktop_1300',
        'PreTest_RK3568_Basic_Desktop_1400',
        # PreTest_RK3568_Basic_Desktop_1500依赖Desktop1300 & Desktop1400
        'PreTest_RK3568_Basic_Desktop_1500',
        'PreTest_RK3568_Tooling_Memorandum_0100',
        'PreTest_RK3568_Tooling_Memorandum_0300',
        'PreTest_RK3568_Tooling_Memorandum_0400',
        'PreTest_RK3568_Tooling_Memorandum_0500',
        'PreTest_RK3568_Tooling_Memorandum_0600',
        'PreTest_RK3568_Tooling_Memorandum_0700',
        'PreTest_RK3568_Tooling_Memorandum_0800',
        'PreTest_RK3568_Tooling_Memorandum_0900',
        'PreTest_RK3568_Tooling_Memorandum_1400',
        'PreTest_RK3568_Tooling_Memorandum_1500',
        'PreTest_RK3568_Basic_Demo_0100',
        'PreTest_RK3568_Tooling_Clock_0100',
    ],
    [
        'PreTest_RK3568_Basic_hap_0100',
        'PreTest_RK3568_Basic_hap_0200',
        'PreTest_RK3568_Multimedia_Music_0100',
        'PreTest_RK3568_Multimedia_Music_0200',
        'PreTest_RK3568_Multimedia_Music_0300',
        'PreTest_RK3568_Multimedia_Music_0400',
        'PreTest_RK3568_Multimedia_Music_0500',
        'PreTest_RK3568_Basic_Setting_0100',
        'PreTest_RK3568_Basic_Setting_0800',
        # todo 需要重启的用例先下线
        # 'PreTest_RK3568_Basic_Setting_2000',
        # 'PreTest_RK3568_Basic_Setting_2100',
        'PreTest_RK3568_Basic_Setting_2600',
        'PreTest_RK3568_Basic_Setting_2700',
        'PreTest_RK3568_Basic_Setting_2800',
        # todo 设置里面的安全子菜单已被移除，用例先下掉
        # 'PreTest_RK3568_Basic_Setting_2900',
        # 'PreTest_RK3568_Basic_Setting_3000',
        # 'PreTest_RK3568_Basic_Setting_3100',
        # 'PreTest_RK3568_Basic_Setting_3200',
        'PreTest_RK3568_Basic_Setting_3300',
        'PreTest_RK3568_Basic_Setting_3500',
        'PreTest_RK3568_Basic_Setting_3800',
        # 3900依赖3800
        'PreTest_RK3568_Basic_Setting_3900',
        'PreTest_RK3568_Basic_Setting_4000',
        'PreTest_RK3568_Basic_Setting_4100',
        'PreTest_RK3568_Basic_Setting_4200',
        'PreTest_RK3568_Basic_Setting_4300',
        'PreTest_RK3568_Basic_Setting_4400',
        'PreTest_RK3568_Basic_Setting_5000',
        'PreTest_RK3568_Basic_Setting_5100',
        'PreTest_RK3568_Basic_Setting_5300',
        'PreTest_RK3568_Basic_Setting_5600',
        'PreTest_RK3568_Basic_Systemui_0400',
        'PreTest_RK3568_Basic_Systemui_0800',
        'PreTest_RK3568_Communication_SMSMMS_0100',
        'PreTest_RK3568_Multimedia_Gallery_0100',
        'PreTest_RK3568_Basic_Keyboard_0100',
        'PreTest_RK3568_Basic_PowerSystem_0100'
    ]
]


def distribute_testcases():
    ip = socket.gethostbyname(socket.gethostname())
    if ip.endswith('4.68'):
        return smoke_cases
    return total_cases


if __name__ == '__main__':
    # 跑冒烟还是预测试用例二选一
    run_cases = distribute_testcases()
    parser = argparse.ArgumentParser()
    parser.add_argument('--config', type=str)
    parser.add_argument('--test_num', type=str, default='1/1')
    parser.add_argument('--tools_path', type=str)
    parser.add_argument('--anwser_path', type=str)
    parser.add_argument('--save_path', type=str)
    parser.add_argument('--device_num', type=str)
    parser.add_argument('--pr_url', type=str)
    args = parser.parse_args()

    if not args.device_num:
        print('PreTest: End of check, test failed!')
        sys.exit(98)

    sn = args.device_num
    cmd = 'run -ta screenshot:true -sn {}'.format(sn)

    report_path = args.save_path

    if args.test_num == '1/1':
        cmd += ' -l {}'.format(';'.join(run_cases[0] + run_cases[1]))
    elif args.test_num == '1/2':
        cmd += ' -l {}'.format(';'.join(run_cases[0]))
    elif args.test_num == '2/2':
        cmd += ' -l {}'.format(';'.join(run_cases[1]))

    report_path = args.save_path + os.path.sep + sn
    cmd += ' -rp {}'.format(report_path)

    # 测试参数
    # cmd = 'run -ta screenshot:true -l {}'.format(';'.join(total_cases[1]))
    main_process(cmd)
    print('PreTest: End of check, test end!')
    sys.exit(0)
